package com.tanhua.jym.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.jym.mapper.DaySumMapper;
import com.tanhua.jym.mapper.InfoMapper;
import com.tanhua.jym.mapper.LogSumMapper;
import com.tanhua.jym.mapper.UserMapper;
import com.tanhua.jym.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DaySumService {
    @Autowired
    private DaySumMapper daySumMapper;

    @Autowired
    private LogSumMapper logSumMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private InfoMapper infoMapper;
    public IPage<AnalysisByDay> findAll(Integer page, Integer pageSize) {
        IPage<AnalysisByDay> infoPage = new Page<>(page, pageSize);
        QueryWrapper<AnalysisByDay> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("record_date");
        this.daySumMapper.selectPage(infoPage, queryWrapper);
        List<AnalysisByDay> records = infoPage.getRecords();
        for (AnalysisByDay record : records) {

            QueryWrapper<User> queryWrapper11 = new QueryWrapper<>();
            queryWrapper11.like("created", record.getRecordDate());
            List<User> users = this.userMapper.selectList(queryWrapper11);
            if (users.size() == 0){
                record.setNumJihuo(0);
                continue;
            }
            QueryWrapper<UserInfoVo> queryWrapper22 = new QueryWrapper<>();
            queryWrapper22.in("user_id",CollUtil.getFieldValues(users,"id"));
            Integer integer = this.infoMapper.selectCount(queryWrapper22);
           record.setNumJihuo(integer);
        }
        return infoPage;
    }

    public List<Retention> findRetention(String start, String end) {
        long between = DateUtil.between(DateUtil.parseDate(end), DateUtil.parseDate(start), DateUnit.DAY);
        int value = new Long(between).intValue();
        ArrayList<Retention> retentions = new ArrayList<>();
        //获取查询范围
        for (int i = value; i > 0; i--) {
            Retention retention = new Retention();
            DateTime now = DateUtil.offsetDay(DateUtil.parseDate(start), i-1);
            retention.setDate(DateUtil.format(now, "yyyy-MM-dd"));
            //获取注册数
            QueryWrapper<AnalysisByDay> query = new QueryWrapper<>();
            query.eq("record_date", now);
            AnalysisByDay analysisByDay = this.daySumMapper.selectOne(query);
            //如果当天无记录,跳过
            //如果当天无注册,返回0
            if (analysisByDay == null || analysisByDay.getNumRegistered() == 0) {
                getNullReturn(retentions, retention);
                continue;
            }
            int sum = analysisByDay.getNumRegistered();
            Double registerNum = Double.valueOf(sum);
            retention.setSum(registerNum);
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("created", DateUtil.format(now, "yyyy-MM-dd"));
            //获取当天注册人id
            List<User> users = this.userMapper.selectList(queryWrapper);
            if (!CollUtil.isNotEmpty(users)) {
                getNullReturn(retentions, retention);
                continue;
            }
            //获取当天注册人的最新登录时间
            QueryWrapper<Log> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.orderByAsc("log_time").in("user_id", CollUtil.getFieldValues(users, "id"))
                    .eq("type", "01");
            List<Log> logs = this.logSumMapper.selectList(queryWrapper1);
            //去除重复登录
            ArrayList<Long> logs1 = new ArrayList<>();
            for (int i1 = logs.size() - 1; i1 >= 0; i1--) {
                if (logs1.contains(logs.get(i1).getUserId())) {
                    logs.remove(i1);
                }
                logs1.add(logs.get(i1).getUserId());
            }
            ArrayList<Double> doubles = new ArrayList<>();
            //0 注册数 8 day30
            for (int j = 0; j < 9; j++) {
                doubles.add(j, 0D);
            }
            for (Log log : logs) {
                //正常情况为登录时间大于等于注册时间 此数据有误
                long between1 = DateUtil.between(DateUtil.parseDate(log.getLogTime()), now, DateUnit.DAY);
                int days = new Long(between1).intValue();
                //注册数
                if (days == 0) {
                    doubles.set(0, doubles.get(0) + 1);
                }
                for (int h = 1; h < 8; h++) {
                    if (days >= h) {
                        doubles.set(h, doubles.get(h) + 1);
                    }
                }
                if (days >= 30) {
                    doubles.set(8, doubles.get(8) + 1);
                }
            }
            retention.setDay01(String.format("%.2f", ((doubles.get(1) - doubles.get(0)) / registerNum) * 100) + "%");
            retention.setDay02(String.format("%.2f", ((doubles.get(2) - doubles.get(0)) / registerNum) * 100) + "%");
            retention.setDay03(String.format("%.2f", ((doubles.get(3) - doubles.get(0)) / registerNum) * 100) + "%");
            retention.setDay04(String.format("%.2f", ((doubles.get(4) - doubles.get(0)) / registerNum) * 100) + "%");
            retention.setDay05(String.format("%.2f", ((doubles.get(5) - doubles.get(0)) / registerNum) * 100) + "%");
            retention.setDay06(String.format("%.2f", ((doubles.get(6) - doubles.get(0)) / registerNum) * 100) + "%");
            retention.setDay07(String.format("%.2f", ((doubles.get(7) - doubles.get(0)) / registerNum) * 100) + "%");
            retention.setDay30(String.format("%.2f", ((doubles.get(8) - doubles.get(0)) / registerNum) * 100) + "%");
            retentions.add(retention);
        }
        return retentions;
    }

    private void getNullReturn(ArrayList<Retention> retentions, Retention retention) {
        retention.setDay01("0");
        retention.setDay02("0");
        retention.setDay03("0");
        retention.setDay04("0");
        retention.setDay05("0");
        retention.setDay06("0");
        retention.setDay07("0");
        retention.setDay30("0");
        retention.setSum(0d);
        retentions.add(retention);
    }

}
